package com.nmgs.util;

import org.springframework.boot.system.ApplicationHome;
import org.springframework.context.annotation.Bean;
import org.springframework.util.ClassUtils;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.*;
import java.util.Enumeration;
import java.util.Properties;

public class PathUtil {
    // 获取jar包所在目录
    public static String jarPath = "";
    public static String applicationPath = "";
    public static String applicationTextPath = "";
    public static String projectApplicationPath;
    public static String webPath = "";
    public static String versionPath;
   // public static String staticResourcesPath;

    static {
        Properties props = System.getProperties();
        String os = props.getProperty("os.name").toLowerCase();
        try {
            ApplicationHome ah = new ApplicationHome(PathUtil.class);
            jarPath = ah.getSource().getParentFile().getAbsolutePath();
           // projectApplicationPath = URLDecoder.decode(ClassUtils.getDefaultClassLoader().getResource("").getPath(), "UTF-8") + "static/application.properties";
            projectApplicationPath = URLDecoder.decode(ClassUtils.getDefaultClassLoader().getResource("").getPath(), "UTF-8") + "application.properties";
            versionPath = URLDecoder.decode(ClassUtils.getDefaultClassLoader().getResource("").getPath(), "UTF-8") + "version/";
            webPath = URLDecoder.decode(ClassUtils.getDefaultClassLoader().getResource("").getPath(), "UTF-8") + "static";
          /* System.out.println("jarPath====="+jarPath);
           System.out.println("projectApplicationPath====="+projectApplicationPath);
           System.out.println("versionPath====="+versionPath);
           System.out.println("webPath====="+webPath);*/
        } catch (UnsupportedEncodingException e) {
            projectApplicationPath = ClassUtils.getDefaultClassLoader().getResource("").getPath() + "application.properties";
            versionPath = ClassUtils.getDefaultClassLoader().getResource("").getPath() + "version/";
            webPath = ClassUtils.getDefaultClassLoader().getResource("").getPath() + "static";
        }
    }
    public static String hostAddress;
    public static String WebServiceWarPath;
    public static String tomcatUserXMLPath;
    public static String webName = "/dist";
    public static Properties properties =new Properties();

    static {
        InputStream resourceAsStream = Bean.class.getClassLoader().getResourceAsStream("application.properties");
        try {
            properties.load(resourceAsStream);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        Properties props = System.getProperties();
        String os = props.getProperty("os.name").toLowerCase();
        if (os.startsWith("win")) {
            webPath = webPath.substring(1) + webName;
        } else {
            webPath = webPath + webName;
        }
        applicationPath = jarPath +File.separator+ "systemConf"+File.separator+"application.properties";
        applicationTextPath = jarPath +File.separator+"systemConf"+File.separator+"applicationText.properties";
        WebServiceWarPath = jarPath + "/../WebServiceWar";
        tomcatUserXMLPath = jarPath + "\\conf/tomcat-users.xml";
        hostAddress = String.valueOf(getInet4Address());
        /*System.out.println("applicationPath========"+applicationPath);
        System.out.println("applicationTextPath========"+applicationTextPath);
        System.out.println("WebServiceWarPath========"+WebServiceWarPath);
        System.out.println("tomcatUserXMLPath========"+tomcatUserXMLPath);
        System.out.println("hostAddress========"+hostAddress);*/
        if (hostAddress.charAt(0) == '/' || hostAddress.charAt(0) == '\\') {
            hostAddress = hostAddress.substring(1);
        }
    }

    public static InetAddress getInet4Address() {
        try {
            //获取所有网络接口
            Enumeration<NetworkInterface> allNetworkInterfaces = NetworkInterface.getNetworkInterfaces();
            //遍历所有网络接口
            for (; allNetworkInterfaces.hasMoreElements(); ) {
                NetworkInterface networkInterface = allNetworkInterfaces.nextElement();
                //如果此网络接口为 回环接口 或者 虚拟接口(子接口) 或者 未启用 或者 描述中包含VM
                if (networkInterface.isLoopback() || networkInterface.isVirtual() || !networkInterface.isUp() || networkInterface.getDisplayName().contains("VM")) {
                    //继续下次循环
                    continue;
                }
                //如果不是Intel与Realtek的网卡
//                if(!(networkInterface.getDisplayName().contains("Intel"))&&!(networkInterface.getDisplayName().contains("Realtek"))){
//                         //继续下次循环
//                            continue;
//                }
                //遍历此接口下的所有IP（因为包括子网掩码各种信息）
                for (Enumeration<InetAddress> inetAddressEnumeration = networkInterface.getInetAddresses(); inetAddressEnumeration.hasMoreElements(); ) {
                    InetAddress inetAddress = inetAddressEnumeration.nextElement();
                    //如果此IP不为空
                    if (inetAddress != null) {
                        //如果此IP为IPV4 则返回
                        if (inetAddress instanceof Inet4Address) {
                            return inetAddress;
                        }
                        /*
                       // -------这样判断IPV4更快----------
                        if(inetAddress.getAddress().length==4){
                            return inetAddress;
                        }

                         */

                    }
                }


            }
            return null;

        } catch (SocketException e) {
            e.printStackTrace();
            return null;
        }
    }
}
